<template>
    <div v-if="tkIsShow">
        <div class="zhezhao"></div>
        <div class="tankuang">
            <div class="tk_title">{{ title }}</div>
            <img src="../assets/icon-close.png" class="tk_close" @click="closeTk">
            <div class="tk_content">
                <el-button type="primary" @click="edit(0,'add')" class="addBTN">新建二级分类</el-button>
                <el-table
                        :data="tableData"
                        style="width: 100%;"
                        height="90%">
                        <el-table-column
                        prop="name"
                        label="名称"
                        width="200">
                        </el-table-column>
                        <el-table-column
                        label="是否显示"
                        width="200">
                        <template slot-scope="scope">
                            <span>{{ scope.row.isShow==1?'显示':'不显示' }}</span>
                            <el-button @click="openTips(scope.row.id,1,scope.row.isShow)" type="text" size="medium " style="margin-left: 10px;color: #FF8F00;">{{ scope.row.isShow==0?'显示':'隐藏' }}</el-button>
                        </template>
                        </el-table-column>
                        <el-table-column
                        prop="level"
                        label="排序（数字越大越靠前）"
                        width="200">
                        </el-table-column>
                        <el-table-column
                        label="操作"
                        width="250">
                        <template slot-scope="scope">
                            <el-button @click="edit(scope.row.id,'edit')" type="text" size="medium ">编辑</el-button>
                            <el-button @click="openTips(scope.row.id,2,0)" type="text" size="medium " style="color: #FF0101;">删除</el-button>
                        </template>
                        </el-table-column>
                    </el-table>
                    <pagination
                    :total="total"
                    :limit="pageSize"
                    :page="pageNumber"
                    @pageChange="pageChange"
                    ></pagination>
            </div>
        </div>
        <tips-box
          ref="tipsBox"
          @confirmTip="confirmTip"
        ></tips-box>
        <add-sort
            ref="addSort"
            @close="
                getData();
            "
        ></add-sort>
    </div>
</template>
<script>
import Pagination from "@/components/pagination.vue";
import tipsBox from "@/components/tipsBox.vue";
import addSort from "@/components/addSort.vue";
export default {
    components: {
    Pagination,
    tipsBox,
    addSort,
  },
  data() {
    return {
        total: 0,
        pageSize: 30,
        pageNumber: 1,
        tkIsShow:false,
        title:"",
        parentId:0,
        tableData:[]
    };
  },
  methods: {
    __init(id) {
      this.parentId=id;
      this.getData();
    },
    getData(){
        var url="manager/getSortByPage";
        var params={
            pageSize: this.pageSize,
            pageNum: this.pageNumber,
            parentId:this.parentId
        }
        this.$post(url, params).then((res) => {
            console.log(res)
            if (res.code === 0) {
            this.total = res.data.total;
            this.tableData=res.data.list;
            } else {
            this.$message.error(res.msg)
            }
        });
    },
    closeTk(){
        this.tkIsShow = false;
        this.$emit("close");
    },
    openTips(id,type,ishow){
        this.editForm={
            id:id,
            type:type,
            ishow:ishow
        }
        var tipContent="";
        if(type==1){
            tipContent="确定修改此分类的显示状态吗？"
        }else if(type==2){
            tipContent="确定要删除此分类吗？"
        }
        if (this.$refs.tipsBox) {
            // 显示当前详情
            this.$refs.tipsBox.tkIsShow = true;
            this.$refs.tipsBox.title = "温馨提示";
            this.$refs.tipsBox.tipContent=tipContent;
        }
    },
    closeTips(){
        if (this.$refs.tipsBox) {
            // 显示当前详情
            this.$refs.tipsBox.tkIsShow = false;
        }
    },
    confirmTip(){
        if(this.editForm.type==1){
            //执行修改显示状态
            this.updateSortStatusById();
        }else if(this.editForm.type==2){
            //执行删除
            this.deleteSortById();
        }
    },
    updateSortStatusById(){
        var url="manager/updateSortStatusById";
        var params={
            id: this.editForm.id,
            isShow: this.editForm.ishow
        }
        this.$post(url, params).then((res) => {
            console.log(res)
            if (res.code === 0) {
                this.$message.success(res.msg)
                this.getData();
                this.closeTips();
            } else {
                this.$message.error(res.msg)
            }
        });
    },
    deleteSortById(){
        var url="manager/deleteSortById";
        var params={
            id: this.editForm.id
        }
        this.$post(url, params).then((res) => {
            console.log(res)
            if (res.code === 0) {
                this.$message.success(res.msg)
                this.getData();
                this.closeTips();
            } else {
                this.$message.error(res.msg)
            }
        });
    },
    edit(id,str){
        if(str=='add'){
            if (this.$refs.addSort) {
                this.$refs.addSort.tkIsShow = true;
                this.$refs.addSort.title = "新增二级分类";
                this.$refs.addSort.__init("add",0,this.parentId,2);
            }
        }else{
            if (this.$refs.addSort) {
                this.$refs.addSort.tkIsShow = true;
                this.$refs.addSort.title = "编辑二级分类";
                this.$refs.addSort.__init("edit",id,this.parentId,2);
            }
        }
    }
  }
}
</script>
<style scoped lang="scss">
  .zhezhao{
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: #000;
    opacity: 0.2;
    top: 0;
    left: 0;
    z-index: 100;
  }
  .tankuang{
    position: fixed;
    width: 50%;
    height: 60%;
    background-color: #fff;
    top: 15%;
    left: 25%;
    z-index: 101;
    border-radius: 5px;
  }
  .tk_title{
    font-size: 17px;
    margin: 15px 0;
    height: 25px;
    line-height: 25px;
  }
  .tk_content{
    width: 90%;
    margin-left: 5%;
    height: calc(100% - 55px);
  }
  .tk_close{
    position: absolute;
    width: 16px;
    height: 16px;
    top: 18px;
    right: 20px;
  }
  .addBTN{
    position: absolute;
    top: 10px;
    left: 2%;
  }
</style>